package red.action;

import com.opensymphony.xwork2.Action;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import red.mapper.Usermapper;
import red.model.UserModel;
import red.service.UserService;
import red.service.impl.UserServiceImpl;
import red.util.BaseAction;
import red.util.Model;
import red.util.SqlSessionFactoryUtil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class UserAction  extends BaseAction {
    private UserService userService=new UserServiceImpl();
    private UserModel userModel;
    private List<UserModel> userModels;
    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public UserModel getUserModel() {
        return userModel;
    }

    public void setUserModel(UserModel userModel) {
        this.userModel = userModel;
    }

    public List<UserModel> getUserModels() {
        return userModels;
    }

    public void setUserModels(List<UserModel> userModels) {
        this.userModels = userModels;
    }

    public String adduser() throws IOException {
        Model model = getParams();
        SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtil.getSqlSessionFactory();
        SqlSession session = sqlSessionFactory.openSession();
        Usermapper usermapper = session.getMapper(Usermapper.class);
        userModel = userService.addUser(model,usermapper);
        //提交事务
        session.commit();
        //关闭session
        session.close();
        return Action.SUCCESS;
    }
    public String getUser(){
        userModels=new ArrayList<>();
        return Action.SUCCESS;
    }
}
